<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>2-ROS通信编程 | ChenBlue | 飞雪连天射白鹿 笑书神侠倚碧鸳</title>

  
  <meta name="author" content="ChenBlue">
  

  
  <meta name="description" content="学习笔记">
  

  
  
  <meta name="keywords" content="ROS">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="2-ROS通信编程"/>

  <meta property="og:site_name" content="ChenBlue"/>

  
  <meta property="og:image" content="/blog/favicon.ico"/>
  

  <link href="/blog/favicon.ico" rel="icon">
  <link rel="alternate" href="/blog/atom.xml" title="ChenBlue" type="application/atom+xml">
  <link rel="stylesheet" href="/blog/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/blog/">ChenBlue</a>
    </h1>
    <p class="site-description">飞雪连天射白鹿 笑书神侠倚碧鸳</p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>2-ROS通信编程</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/blog/2021/04/12/2-ROS通信编程/" rel="bookmark">
        <time class="entry-date published" datetime="2021-04-12T12:39:03.000Z">
          2021-04-12
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h1 id="ROS-topic-编程"><a href="#ROS-topic-编程" class="headerlink" title="ROS topic 编程"></a>ROS topic 编程</h1><p>创建一个ROS工作空间并在工作空间内创建src源码路径</p>
<p>初始化这个工作空间</p>
<pre class="language-none"><code class="language-none">mkdir  -p catkin_ws&#x2F;src
cd catkin_ws&#x2F;src
catkin_init_workspace
cd ..
catkin_make</code></pre>
<p>在工作空间的根目录下编译并 设置环境变量 并检查是否设置正确</p>
<pre class="language-none"><code class="language-none">source devel&#x2F;setup.zsh
echo $ROS_PACKAGE_PATH</code></pre>

<h2 id="ROS-项目开发流程"><a href="#ROS-项目开发流程" class="headerlink" title="ROS 项目开发流程"></a>ROS 项目开发流程</h2><p><strong>$ catkin_create_pkg <package_name> [depend1] [depend2] [depend3]</package_name></strong></p>
<h4 id="创建并编译功能包"><a href="#创建并编译功能包" class="headerlink" title="创建并编译功能包"></a>创建并编译功能包</h4><pre class="language-none"><code class="language-none">cd catkin_ws&#x2F;src
catkin_create_pkg learning_communication rospy roscpp std_msgs std_srvs
cd ..   &#x2F;&#x2F;(编译一定是在工作空间的根目录)
catkin_make
source devel&#x2F;setup.zsh</code></pre>

<p>新增代码之后需要修改 ==CMakeLists.txt== </p>
<ol>
<li>设置需要编译的代码和生成可执行文件</li>
<li>设置链接库</li>
</ol>
<pre class="language-none"><code class="language-none">vi catkin_ws&#x2F;src&#x2F;learning_communication&#x2F;CMakeLists.txt

add_executable(string_publisher src&#x2F;string_publisher.cpp)
target_link_libraries(string_publisher $&#123;catkin_LIBRARIES&#125;)

add_executable(string_subscriber src&#x2F;string_subscriber.cpp)
target_link_libraries(string_subscriber $&#123;catkin_LIBRARIES&#125;)</code></pre>
<p>运行、检验</p>
<pre class="language-none"><code class="language-none">source devel&#x2F;setup.zsh

roscore &amp;

rosrun learning_communication string_publisher
rosrun learning_communication string_subscriber   &#x2F;&#x2F;新建一个终端</code></pre>
<h4 id="自定义-msg"><a href="#自定义-msg" class="headerlink" title="自定义 msg"></a>自定义 msg</h4><p>创建 msg 文件夹及文件</p>
<pre class="language-none"><code class="language-none">cd catkin_ws&#x2F;src&#x2F;learning_communication
mkdir msg
cd msg
touch PersonMsg.msg</code></pre>
<p>在 PersonMsg.msg 添加内容</p>
<pre class="language-none"><code class="language-none">string name
uint8  age
uint8  sex

uint8 unknown &#x3D; 0
uint8 male    &#x3D; 1
uint8 female  &#x3D; 2</code></pre>

<p>在 ==pachage.xml== 中添加功能包依赖</p>
<pre class="language-none"><code class="language-none">&lt;build_depend&gt;message_generation&lt;&#x2F;build_depend&gt;
&lt;exec_depend&gt;message_runtime&lt;&#x2F;exec_depend&gt;</code></pre>

<p>在 ==CMakeLists.txt==  中添加编译选项</p>
<p><code>find_package(... message_generation)</code></p>
<p><code>catkin_package(... message_runtime)</code></p>
<pre class="language-none"><code class="language-none">add_message_files(FILES PersonMsg.msg)
generate_messages(DEPENDENCIES std_msgs)</code></pre>

<pre class="language-none"><code class="language-none">add_executable(person_publisher src&#x2F;person_publisher.cpp)
target_link_libraries(person_publisher $&#123;catkin_LIBRARIES&#125;)
add_dependencies(person_publisher $&#123;PROJECT_NAME&#125;_gencpp)

add_executable(person_subscriber src&#x2F;person_subscriber.cpp)
target_link_libraries(person_subscriber $&#123;catkin_LIBRARIES&#125;)
add_dependencies(person_subscriber $&#123;PROJECT_NAME&#125;_gencpp)</code></pre>

<p>运行、检验</p>
<pre class="language-none"><code class="language-none">source devel&#x2F;setup.zsh
roscore &amp;

rosrun learning_communication person_publisher
rosrun learning_communication person_subscriber   &#x2F;&#x2F;新建一个终端</code></pre>

<h1 id="ROS-service-编程"><a href="#ROS-service-编程" class="headerlink" title="ROS service 编程"></a>ROS service 编程</h1><h4 id="自定义-srv"><a href="#自定义-srv" class="headerlink" title="自定义 srv"></a>自定义 srv</h4><p>创建 msg 文件夹及文件</p>
<pre class="language-none"><code class="language-none">cd catkin_ws&#x2F;src&#x2F;learning_communication
mkdir srv
cd srv
touch PersonSrv.srv</code></pre>
<p>在 PersonSrv.srv 添加内容</p>
<pre class="language-none"><code class="language-none">string name
uint8  age
uint8  sex

uint8 unknown &#x3D; 0
uint8 male    &#x3D; 1
uint8 female  &#x3D; 2

---
string result</code></pre>
<p>三横线以上是 request 内容 以下是 respond 内容</p>
<p>在 ==CMakeLists.txt== 添加一行</p>
<pre class="language-none"><code class="language-none">add_service_files(FILES PersonSrv.srv)</code></pre>
<p>其实还应该在package.xml中添加依赖。以及链接库，由于msg已经添加过，所以可以省</p>
<p>ROS查看节点类型一节该类型的结构体成员</p>
<pre class="language-none"><code class="language-none">rostopic type &#x2F;turtle1&#x2F;pose
rosmsg show turtlesim&#x2F;Pose

rostopic type &#x2F;turtle1&#x2F;cmd_vel
rosmsg show geometry_msgs&#x2F;Twist</code></pre>
<p><a target="_blank" rel="noopener" href="https://gitee.com/ChenBlue/linux/tree/master/ros/turtle_cmd/src">ROS topic code</a></p>

      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    
      

    <span class="post-categories">
      <i class="icon-categories"></i>
        <a href="/blog/categories/ROS-学习笔记/">ROS 学习笔记</a>
    </span>
    

    
    

    <span class="post-tags">
      <i class="icon-tags"></i>
        <a href="/blog/tags/ROS/">ROS</a>
    </span>
    

    </div>

    
  </div>
</article>

  









    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2023 ChenBlue
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>